﻿__author__ = 'YT'

from .component import map
from . import AStarSearch, tool

# 定义一个敌人类
class EnemyInfo():
    # 初始化敌人类
    def __init__(self, enemy):
        # 定义敌人
        self.enemy = enemy
        # 定义敌人攻击范围
        self.range_num = 100
        # 定义敌人攻击间隔
        self.kill_time = 1000
        # 定义敌人是否远程
        self.remote = False
         
def getAction(entity, map, enemy_group):
    # 定义一个函数，用于获取目标位置
    def getDestination(entity, map, enemy):
        # 获取可攻击位置
        dir_list = tool.getAttackPositions(enemy.map_x, enemy.map_y)
        best_pos = None
        min_dis = 0
        # 遍历可攻击位置
        for offset_x, offset_y in dir_list:
            x, y = enemy.map_x + offset_x, enemy.map_y + offset_y
            # 如果位置有效且可移动
            if map.isValid(x, y) and map.isMovable(x, y):
                # 获取A*搜索距离
                distance = AStarSearch.getAStarDistance(map, (entity.map_x, entity.map_y), (x, y))
                # 如果距离为None，跳过
                if distance is None:
                    continue
                # 如果best_pos为None，则更新best_pos
                if best_pos is None:
                    best_pos = (x, y)
                    min_dis =  distance
                # 如果距离小于min_dis，则更新best_pos
                elif distance < min_dis:
                    best_pos = (x, y)
                    min_dis =  distance
        # 返回best_pos
        return best_pos

    def getEnemyInfo(info_list, entity, enemy, destination):
        # 创建一个EnemyInfo对象
        enemyinfo = EnemyInfo(enemy)
        # 使用A*算法搜索路径
        location = AStarSearch.AStarSearch(map, (entity.map_x, entity.map_y), destination)
        # 获取第一步和距离
        _, _, distance = AStarSearch.getFirstStepAndDistance(location)

        # 打印entity和destination的坐标，location的坐标和距离
        print('entity(%d, %d), dest(%d, %d) location(%d, %d) distance:%d' %
            (entity.map_x, entity.map_y, destination[0], destination[1], location.x, location.y, distance))

        # 如果距离为0，range_num为0
        if distance == 0:
            enemyinfo.range_num = 0
        # 否则，range_num为（distance-1）除以entity的range
        else:
            enemyinfo.range_num = (distance - 1) // entity.attr.range
        # 设置enemyinfo的location和distance
        enemyinfo.location = location
        enemyinfo.distance = distance

        # 计算伤害
        hurt = entity.attr.getHurt(enemy.attr)
        # 计算击杀时间
        enemyinfo.kill_time = (enemy.health-1) // hurt

        # 设置enemyinfo的remote
        enemyinfo.remote = enemy.attr.remote
        # 将enemyinfo添加到info_list中
        info_list.append(enemyinfo)
    
    info_list = []
    best_info = None
    # 遍历敌人组
    for enemy in enemy_group:
        # 如果实体相邻，则设置目的地为其自身
        if tool.isNextToEntity(entity, enemy):
            print('entity(%d,%d) next to enemy(%d, %d)' % (entity.map_x, entity.map_y, enemy.map_x, enemy.map_y))
            destination = (entity.map_x, entity.map_y)
        # 如果实体不相邻，则获取目的地
        else:
            destination = getDestination(entity, map, enemy)
        # 如果目的地不为空，则获取敌人信息
        if destination is not None:
            getEnemyInfo(info_list, entity, enemy, destination)

    # 遍历info_list列表中的每一个元素info
    for info in info_list:
        # 如果best_info为空，则将info赋值给best_info
        if best_info == None:
            best_info = info
        else:
            # 如果info的range_num小于best_info的range_num，则将info赋值给best_info
            if info.range_num < best_info.range_num:
                best_info = info
            # 如果info的range_num等于best_info的range_num
            elif info.range_num == best_info.range_num:
                # 如果info的range_num为0
                if info.range_num == 0:
                    # 如果info的kill_time小于best_info的kill_time，则将info赋值给best_info
                    if info.kill_time < best_info.kill_time:
                        best_info = info
                    # 如果info的kill_time等于best_info的kill_time
                    elif info.kill_time == best_info.kill_time:
                        # 如果info的remote为True，best_info的remote为False，则将info赋值给best_info
                        if info.remote == True and best_info.remote == False:
                            best_info = info
                        # 如果info的distance小于best_info的distance，则将info赋值给best_info
                        elif info.distance < best_info.distance:
                            best_info = info
                # 如果info的range_num不为0
                else:
                    # 如果info的distance小于best_info的distance，则将info赋值给best_info
                    if info.distance < best_info.distance:
                        best_info = info
    
    if best_info.range_num == 0:
        # 如果最佳信息点的范围数是0，则返回最佳信息点的坐标和敌人信息
        return (best_info.location.x, best_info.location.y, best_info.enemy)
    elif best_info.range_num == 1:
        # 如果最佳信息点的范围数是1，则获取该点的范围
        range = entity.attr.range
        # 获取该点在范围内的坐标
        x, y = AStarSearch.getPosInRange(best_info.location, range)
        # 返回该坐标和敌人信息
        return (x, y, None)
    else:
        # 如果最佳信息点的范围数大于1，则获取该点到敌人的距离减去自身的范围
        range = best_info.distance - entity.attr.range
        # 获取该点在范围内的坐标
        x, y = AStarSearch.getPosInRange(best_info.location, range)
        # 返回该坐标和敌人信息
        return (x, y, None)